package com.zhentao.product.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.login.service.TUserService;
import com.zhentao.nlz.project.dto.TDeptDto;
import com.zhentao.nlz.project.dto.TTenantDto;
import com.zhentao.nlz.project.login.TUser;
import com.zhentao.nlz.project.product.TTenant;
import com.zhentao.product.mapper.TTenantMapper;
import com.zhentao.product.service.TDeptService;
import com.zhentao.product.service.TTenantService;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 不.凌
* @description 针对表【t_tenant(租户表)】的数据库操作Service实现
* @createDate 2024-09-22 21:09:18
*/
@Service
public class TTenantServiceImpl extends ServiceImpl<TTenantMapper, TTenant>
    implements TTenantService{
    @Autowired
    private TTenantMapper tenantMapper;
    @Autowired
    private TDeptService deptService;
    @Autowired
    private TUserService tUserService;
    @Override
    public Result listPage(TTenantDto tenantDto) {
        Page<TTenant> page = new Page<>(tenantDto.getPageNum(), tenantDto.getPageSize());
        LambdaQueryWrapper<TTenant> wrapper = Wrappers.<TTenant>lambdaQuery()
                .like(tenantDto.getName() != null, TTenant::getName, tenantDto.getName())
                .eq(tenantDto.getStatus() != null, TTenant::getStatus, tenantDto.getStatus());
        this.page(page, wrapper);
        for (TTenant record : page.getRecords()) {
            record.setDeptList(deptService.deptListByTenantId(record.getId()));
        }
        return Result.success(page);
    }

    @Override
    public Result tenantSaveOrUpdate(TTenant tenant) {
        if (tenant.getId()!=null){
            tenant.setUpdateTime(new DateTime());
        }else {
            tenant.setCreateTime(new DateTime());
        }
        boolean b = this.saveOrUpdate(tenant);
        return b?Result.success("成功"):Result.error("失败");
    }

    @Override
    public Result tenantDelete(TTenant tenant) {
        boolean b = this.removeById(tenant);
        return b?Result.success("成功"):Result.error("失败");
    }
}




